﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections;
using Cherry.Agile.RpcService;
using System.Reflection;

namespace Cherry.Agile.RpcService
{
    /// <summary>
    /// 请求上下文信息类
    /// </summary>
    public class ProcessContext
    {
        /// <summary>
        /// 上下文信息
        /// </summary>
        public IDictionary Items { get; protected set; }

        /// <summary>
        /// 创建实例
        /// </summary>
        public ProcessContext()
        {
            Items = new Dictionary<string,object>();
        }


        /// <summary>
        /// 创建实例
        /// </summary>
        public ProcessContext(Action<string> tracerWriter) :this()
        {
            this.TracerWriter = tracerWriter;
        }


        /// <summary>
        /// 业务码
        /// </summary>
        public short BusinessCode { get; set; }

        /// <summary>
        /// 功能码
        /// </summary>
        public short FunctionCode { get; set; }

        /// <summary>
        /// 请求码(为业务码和功能码的组合)
        /// </summary>
        public int RequestCode { get; set; }

        private ResponseCode _responseCode = 0;

        /// <summary>
        /// 响应码
        /// 注意：响应码的赋值为逻辑组合赋值的方式
        /// </summary>
        public ResponseCode ResponseCode 
        {
            get{ return _responseCode;}
            set { _responseCode |= value; }
        }

        /// <summary>
        /// 请求数据读取器
        /// </summary>
        public BufferReader Reader { get; set; }


        /// <summary>
        /// 响应数据的写入器
        /// </summary>
        public BufferWriter Writer { get; set; }


        /// <summary>
        /// 当前使用的通信协议包的版本
        /// </summary>
        public Version Version { get; set; }


        /// <summary>
        /// 文化/语言信息
        /// </summary>
        /// <value></value>
        public CultureInfo Culture { get; set; }

        /// <summary>
        /// 是否通过验证
        /// </summary>
        public bool Authendicated { get; set; }


        ///// <summary>
        ///// 用户帐号
        ///// </summary>
        ///// <value></value>
        //public string UserAccount { get; set; }

        /// <summary>
        /// 用户Token
        /// </summary>
        public long Token { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }


        private Action<string> TracerWriter;

        /// <summary>
        /// 在上下文中写入一条跟踪信息
        /// 是否成功写入取决于跟踪组件是否开启
        /// 跟踪信息输入方式，取决于跟踪输出插件
        /// </summary>
        /// <param name="message">跟踪文本</param>
        public void WriteTrace(string message)
        {
            if (TracerWriter != null)
            {
                TracerWriter(message);
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            int readLen = Reader == null ? 0 : Reader.Buffer.Length,
                writeLen = Writer == null ? 0 : (int)Writer.Length;
            return string.Format(@"Time[{0}] Request[{1},{2}] UserId[{3}] Token[{4}] RequestLen[{5}] ResonpseLength[{6}] Response[{7}]",
                 DateTime.Now,
                 BusinessCode,
                 FunctionCode,
                 UserId,
                 Token,
                 readLen,
                 writeLen,
                 ResponseCode
                 );
        }
    }
}
